﻿namespace VisualSVN.Core.Licensing
{
    using System;
    using System.Runtime.InteropServices;
    using VisualSVN.Core;
    using VisualSVN.Utils;

    public class LicenseConverter
    {
        private const char NewLicenseKeySymbol = 'N';
        private const char OldLicenseKeySymbol = 'C';
        private const char PlainLicenseKeySymbol = 'P';
        private const char ServerLicenseKeySymbol = 'S';

        private static string ExtractKeyBody(string key, out char keyType)
        {
            keyType = ' ';
            if ((key == null) || (key.Length < 1))
            {
                return null;
            }
            keyType = char.ToUpper(key[0]);
            string str = key.Substring(1);
            if (str.Length == 0)
            {
                return null;
            }
            return str;
        }

        public static License KeyToLicense(IDecoder decoder, string key)
        {
            char ch;
            string str = ExtractKeyBody(key, out ch);
            if (str == null)
            {
                return null;
            }
            try
            {
                switch (ch)
                {
                    case 'N':
                    {
                        NewLicenseSerializer serializer3 = new NewLicenseSerializer();
                        return serializer3.Deserialize(str, decoder);
                    }
                    case 'O':
                        goto Label_0073;

                    case 'P':
                    {
                        PlainLicenseSerializer serializer = new PlainLicenseSerializer();
                        return serializer.Deserialize(str);
                    }
                    case 'C':
                    {
                        OldLicenseSerializer serializer2 = new OldLicenseSerializer();
                        return serializer2.Deserialize(str, decoder);
                    }
                }
            }
            catch (Exception exception)
            {
                Log.ReportException(exception);
            }
        Label_0073:
            return null;
        }

        public static ServerLicense KeyToServerLicense(IDecoder decoder, string key)
        {
            char ch;
            string str = ExtractKeyBody(key, out ch);
            if (str != null)
            {
                try
                {
                    switch (ch)
                    {
                        case 'S':
                        {
                            ServerLicenseSerializer serializer = new ServerLicenseSerializer();
                            return serializer.Deserialize(str, decoder);
                        }
                    }
                }
                catch (Exception exception)
                {
                    Log.ReportException(exception);
                }
            }
            return null;
        }

        public static string LicenseToKey(IEncoder encoder, License license)
        {
            string str = new NewLicenseSerializer().Serialize(license, encoder);
            return ('N' + str);
        }

        public static string LicenseToKey(IEncoder encoder, ServerLicense license)
        {
            string str = new ServerLicenseSerializer().Serialize(license, encoder);
            return ('S' + str);
        }

        public static string LicenseToKeyOld(IEncoder encoder, License license)
        {
            string str = new OldLicenseSerializer().Serialize(license, encoder);
            return ('C' + str);
        }
    }
}

